package com.gitee.cirnochat.chat.filter;

import com.gitee.cirnochat.chat.model.enums.ChatSessionType;
import com.gitee.cirnochat.chat.model.vo.req.ChatMessageSendRequest;
import com.gitee.cirnochat.common.exception.MessageBusinessException;
import com.gitee.cirnochat.common.model.enums.ErrorCode;
import com.gitee.cirnochat.user.model.entity.UserPermission;
import com.gitee.cirnochat.user.service.UserService;
import com.gitee.cirnochat.websocket.model.dto.WebSocketUserMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.security.Principal;

/**
 * 消息验证过滤器
 * 用于验证消息内容是否为空
 * @author FlanChan
 * @since 2025/6/28 14:22
 */
@Slf4j
@Component
@RequiredArgsConstructor
@Order(1)
public class ValidateMessageFilter extends AbstractMessageFilter {
    private final UserService userService;

    @Override
    public void doFilter(Object message, Principal principal) {
        if (message instanceof ChatMessageSendRequest req) {
            if (req.getUser().getId() == null || req.getUser().getId() <= 0) {
                throw new MessageBusinessException(
                        ErrorCode.PARAMS_ERROR,
                        req.getMessageId(),
                        "用户信息不完整"
                );
            }
            if (!req.getUser().getId().equals(Long.valueOf(principal.getName()))) {
                throw new MessageBusinessException(
                        ErrorCode.PARAMS_ERROR,
                        req.getMessageId(),
                        "用户信息不匹配"
                );
            }
            if (req.getSessionId() == null || req.getSessionId() <= 0) {
                throw new MessageBusinessException(
                        ErrorCode.PARAMS_ERROR,
                        req.getMessageId(),
                        "会话信息不完整"
                );
            }
            if (req.getUser() == null || req.getUser().getId() <= 0) {
                throw new MessageBusinessException(
                        ErrorCode.PARAMS_ERROR,
                        req.getMessageId(),
                        "用户信息不完整"
                );
            }
            if (req.getSessionType().equals(ChatSessionType.SINGLE)) {
                final UserPermission receiver = userService.getUserPermissionById(req.getSessionId(), Long.valueOf(principal.getName()));
                if (receiver == null) {
                    throw new MessageBusinessException(
                            ErrorCode.NOT_FOUND_ERROR,
                            req.getMessageId(),
                            "接收者用户不存在"
                    );
                }
                // 陌生人判断
                if (receiver.getFriended() == null || !receiver.getFriended()) {
                    if (!receiver.getAllowStrangerToSendMessage()) {
                        throw new MessageBusinessException(
                                ErrorCode.OPERATION_ERROR,
                                req.getMessageId(),
                                "对方未添加你为好友，无法发送消息"
                        );
                    }
                }
                // 黑名单判断
                if (receiver.getBlockeded()) {
                    throw new MessageBusinessException(
                            ErrorCode.OPERATION_ERROR,
                            req.getMessageId(),
                            "对方已将你拉黑，无法发送消息"
                    );
                }
                super.doFilter(message,principal);
            }
        }
    }
}